package com.zcc.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zcc.blog.entity.TbCategoryBlog;
import com.zcc.blog.mapper.TbCategoryBlogMapper;
import com.zcc.blog.service.TbCategoryBlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcc.blog.untils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcc
 * @since 2022-09-11
 */
@Service
public class TbCategoryBlogServiceImpl extends ServiceImpl<TbCategoryBlogMapper, TbCategoryBlog> implements TbCategoryBlogService {
    @Autowired
    TbCategoryBlogMapper tbCategoryBlogMapper;

    @Override
    public List<TbCategoryBlog> findAllByCategoryId(Long categoryId) {
        return tbCategoryBlogMapper.findAllByCategoryId(categoryId);
    }

    @Override
    public List<TbCategoryBlog> findAllByBlogId(Long blogId) {
        return tbCategoryBlogMapper.findAllByBlogId(blogId);
    }

    @Override
    public Result addTbCategoryBlog(TbCategoryBlog tbCategoryBlog) {
        TbCategoryBlog temp = new TbCategoryBlog();
        BeanUtil.copyProperties(tbCategoryBlog,temp,"id");
        boolean save = this.save(temp);
        if (save) {
            return Result.success(null);
        }
        return Result.fail("添加失败");
    }

    @Override
    public Result updateTbCategoryBlog(TbCategoryBlog tbCategoryBlog) {
        boolean save = this.save(tbCategoryBlog);
        if (save) {
            return Result.success(null);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result deleteTbCategoryBlog(Long id) {
        boolean b = tbCategoryBlogMapper.changeStatus(1, id);
        if (b) {
            return Result.success(null);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result recoverTbCategoryBlog(Long id) {
        boolean b = tbCategoryBlogMapper.changeStatus(0, id);
        if (b) {
            return Result.success(null);
        }
        return Result.fail("删除失败");
    }
}
